﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Pakxel.Data;
using System.Linq;
using System.Diagnostics;
using Telerik.WinControls.UI;
using Pakxel.Framework;
namespace Pakxel.SmartLogistic
{
    public partial class BaseWorkBench : Telerik.WinControls.UI.RadForm
    {
        //public delegate void onRecordSelected(object sender, Dummy e);
        //public event onRecordSelected RecordSelected;
        protected delegate bool RuleManager(string CString);
        protected Pakxel.Framework.Rules ruleToCheck;  
        protected RuleManager RuleChecker;
        protected RuleManager IsNumeric;
        protected MEDataDataContext DataContext;
        protected T005 Viaje;
        internal UIHelper uiHelper;
        public BaseWorkBench()
        {
            InitializeComponent();
        }

        private void BaseWorkBench_Load(object sender, EventArgs e)
        {
           uiHelper = new UIHelper();
            WorkBenchFilter.TextBox.BackColor = Color.LightYellow;
            FilterSelector.SelectedIndex = 0;
            ruleToCheck = new Pakxel.Framework.Rules();
            // Ejemplo de Uso
            RuleChecker += new RuleManager(ruleToCheck.IsDigit);
            RuleChecker += new RuleManager(ruleToCheck.IsDummy);
            // Para el No Viaje
            IsNumeric += new RuleManager(ruleToCheck.IsNumeric);  
        }

        internal void ClearControls(Control.ControlCollection Controles)
        {
           uiHelper.ClearControls(Controles); 
            //foreach (var Ctl in Controlsx)
            //{
            //    switch (Ctl.GetType().Name)
            //    {
            //        case "RadDateTimePicker":
            //            ((RadDateTimePicker)Ctl).Value = DateTime.Now; break;
            //        case "RadSpinEditor":
            //            ((RadSpinEditor)Ctl).Value = ((RadSpinEditor)Ctl).Minimum; break;
            //        case "RadMultiColumnComboBox":
            //            ((RadMultiColumnComboBox)Ctl).SelectedIndex = -1; break;
            //        case "RadDropDownList":
            //            ((RadDropDownList)Ctl).SelectedIndex = -1; break;
            //        case "RadRadioButton":
            //            ((RadRadioButton)Ctl).IsChecked = false; break;
            //        case "RadTextBox":
            //            ((RadTextBox)Ctl).Text = ""; break;
            //        case "RadGroupBox": ClearControls(((RadGroupBox)Ctl).Controls); break;

            //    }
            //}
        }

        internal void ShowViajeInfo(T005 Viaje)
        {
            if (Viaje != null)
            {
                NoViaje.Text = Viaje.T005NoViaje.ToString();
                FechaViaje.Text = Viaje.T005FechaInicio.ToShortDateString();
                ViajeCargado.IsChecked = Viaje.T005CargadoVacio.Equals('C') ? true : false;
                ViajeVacio.IsChecked = Viaje.T005CargadoVacio.Equals('V') ? true : false;
                TipSerImpo.IsChecked = Viaje.T005TipSer.Equals('I') ? true : false;
                TipSerExpo.IsChecked = Viaje.T005TipSer.Equals('E') ? true : false;

                Unidad.Text = Viaje.T006NoUnidad;
                Remolque.Text = Viaje.T007IdRemolque;
                var origen = (from O in DataContext.T008s where O.T008IdOrigen.Equals(Viaje.T008IdOrigen) select O).SingleOrDefault();
                Origen.Text = origen.T008Nombre;
                var destino = (from D in DataContext.T008s where D.T008IdOrigen.Equals(Viaje.T008IdDestino) select D).SingleOrDefault();
                Destino.Text = destino.T008Nombre;
                Transportista.Text = Viaje.T009IdLinea;
                var Cancelado = Viaje.T005Cancelado;
                var operador = (from Op in DataContext.T010s where Op.T010NoOperador.Equals(Viaje.T010NoOperador) select Op).SingleOrDefault();
                Operador.Text = operador.T010Nombre;
                var cliente = (from C in DataContext.T011s where C.T011Entity.Equals(Viaje.T011Entity) select C).SingleOrDefault();
                Cliente.Text = cliente.T011Nombre;
            }
        }

        private void WorkBenchFilter_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (IsNumeric(WorkBenchFilter.Text))
                {
                    var Sel = FilterSelector.SelectedItem.ToString();
                    switch (Sel)
                    {
                        case "NoViaje":
                            try
                            {
                                DataContext = null;
                                DataContext = new MEDataDataContext(WorkBench.ConnectionString);
                                Viaje = null;
                                Viaje = (from Viajes in DataContext.T005s where Viajes.T005NoViaje.Equals(Convert.ToInt32(WorkBenchFilter.Text)) select Viajes).SingleOrDefault();
                                ShowViajeInfo(Viaje);
                            }
                            catch (SystemException exp)
                            {
                               MainApp.WriteExceptionToLog(exp);
                            }
                            break;
                        case "NoUnidad":
                            break;
                        default:
                            break;
                    }
                }
            }
        }

    }
}
